package com.google.coffeeshop.gui.desktop;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public abstract class AbstractBaseTable extends JTable {
	private static final long serialVersionUID = 1L;

	protected AbstractBaseTable(Object[][] rowData, String[] columnNames) {
		super(rowData, columnNames);
	}
	
	public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
		Component component = super.prepareRenderer(renderer, row, column);
		
		if (row % 2 == 0) {
			component.setBackground(new Color(240, 240, 240));
		} else {
			component.setBackground(Color.WHITE);
		}
		
		if (isCellSelected(row, column)) {
			component.setBackground(Color.GREEN);
		}
		
		return component;
	}
	
	protected Object findModelValue(DefaultTableModel model, String columnName, int rowIndex) {
		return model.getValueAt(rowIndex, model.findColumn(columnName));
	}
	
	protected TableColumn findColumn(DefaultTableModel model, String colmnName) {
		return getColumnModel().getColumn(model.findColumn(colmnName));
	}
	
	@Override
    public boolean isCellEditable(int row, int column) {
       return Boolean.FALSE;
    }
	
}
